/* 
 * Copyright 2014-2021 UME Framework, GNU General Public License 
 */
package com.u2framework.lib.tool.gen.base;

import java.io.File;
import java.io.FileWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

/**
 * BaseVelocityGenerator
 *
 * @author SultansOfSwing & MaYue
 */
public class BaseGenerator {
    /**
     * template
     */
    private Template template;

    /**
     * BaseVelocityGenerator
     * 
     * @param vltTemplate
     * @param charSet
     */
    public BaseGenerator(String vltTemplate) {
        Properties p = new Properties();
        p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        p.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogChute");

        // p.setProperty("resource.loader", "jar");
        // p.setProperty("class.resource.loader.class",
        // "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
        // p.setProperty("jar.resource.loader.path",
        // "jar:file:framework-tool-dbgen.jar");
        Velocity.init(p);
        template = Velocity.getTemplate(vltTemplate);
    }

    /**
     * CodeGenerator
     * 
     * @param vltTemplate
     * @param prop
     */
    public BaseGenerator(String vltTemplate,
            Properties properties) {
        // Off Velocity log
        Velocity.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogChute");
        Velocity.init(properties);
        template = Velocity.getTemplate(vltTemplate);
    }

    /**
     * execute
     * 
     * @param dtoMap
     * @param genSrcPath
     * @param vltTemplate
     * @param charSet
     * @throws Exception
     */
    public void execute(String inputDtoName, Object inputDto, String fileName) throws Exception {
        FileWriter writer = null;
        try {
            writer = new FileWriter(fileName);
            VelocityContext context = new VelocityContext();
            context.put(inputDtoName, inputDto);
            template.merge(context, writer);
            writer.flush();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * createPackageDir
     * 
     * @param rootPath
     * @param packageName
     * @return
     */
    public String createPackageDir(String rootPath, String packageName) {
        rootPath = rootPath.contains("\\") ? rootPath.replace("\\", "/") : rootPath;
        rootPath = rootPath.endsWith("/") ? rootPath : rootPath + "/";
        packageName = packageName.replace(".", "/");
        packageName = rootPath + packageName;
        File p = new File(packageName);
        if (!p.exists()) {
            p.mkdirs();
        }
        return p.getPath() + "/";
    }

    /**
     * createPackageDir
     * 
     * @param rootPath
     * @param packageName
     * @return
     */
    public String createCSharpPackageDir(String rootPath) {
        rootPath = rootPath.contains("\\") ? rootPath.replace("\\", "/") : rootPath;
        rootPath = rootPath.endsWith("/") ? rootPath : rootPath + "/";
        File p = new File(rootPath);
        if (!p.exists()) {
            p.mkdirs();
        }
        return p.getPath() + "/";
    }
}
